

var DamageNumberManager = cc.Class({
    extends: cc.Component,

    properties: {
        damageNumber: cc.Prefab,
		damageNumberPool: {
			default: null,
			type: cc.NodePool,
			visible: false,
		}
    },
	statics: {
		instance: null,
	},
	ctor() {
		DamageNumberManager.instance = this;
	},

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
		this.damageNumberPool = new cc.NodePool();
    },

    // update (dt) {},

	spawnDamageNumber (position, damage) {
		let prefab = null;
		if (this.damageNumberPool.size() > 0) {
			prefab = this.damageNumberPool.get();
		} else {
			prefab = cc.instantiate(this.damageNumber);
		}
		prefab.parent = this.node;
		prefab.setPosition(position);
		prefab.getComponent(cc.Label).string = damage;
		prefab.active = true;
		cc.tween(prefab)
		.call(() => {
			prefab.scale = 0;
		})
		.to(0.5, { scale: 1.5, position: cc.v2(position.x, position.y + 100) }, { easing: 'sineOut' })
		.delay(0.5)
		.call(() => {
			prefab.active = false;
			this.damageNumberPool.put(prefab);
		})
		.start();
	},
});

module.exports = DamageNumberManager;
